package org.starsource.forum.server.domain;

import java.io.Serializable;
import java.util.Iterator;
import java.util.LinkedHashSet;
import java.util.Set;

import org.starsource.forum.client.domain.CategoryObject;
import org.starsource.forum.client.util.Assertion;

public class Category implements HasId, Serializable {

	protected static org.apache.log4j.Logger logger = org.apache.log4j.LogManager
	.getLogger(Category.class.getName());

	private static final long serialVersionUID = 9178394026208625528L;

	private int id;
	
	private ForeignKey<Category> parent = new ForeignKey<Category>();
	
	private String name;
	
	private String caption;
	
	private Set<Category> children = new LinkedHashSet<Category>();
	
	public Category() {
	}
	
	public Category(Category parent, String name, String caption) {
		this.setParent(parent);
		this.name = name;
		this.caption = caption;
	}
	
	public Category(int id, Category parent, String name, String caption) {
		this.id = id;
		this.setParent(parent);
		this.name = name;
		this.caption = caption;
	}
	
	@Override
	public boolean equals(Object obj) {
		try {
			Category object = (Category)obj;
			
			if (this.id != object.getId()) {
				logger.debug("compare failed: this.id=" + this.id + " obj.id=" + object.id);
				return false;
			}
			
			if (this.parent.getId() != object.parent.getId()) {
				logger.debug("compare failed: this.parentId=" + this.parent.getId() + " obj.parentId=" + object.parent.getId());
				return false;
			}
			
			if (!Assertion.isEqual(this.name, object.name)) {
				logger.debug("compare failed: this.name=" + this.name + " obj.name=" + object.name);
				return false;
			}
			
			if (!Assertion.isEqual(this.caption, object.caption)) {
				logger.debug("compare failed: this.caption=" + this.caption + " obj.caption=" + object.caption);
				return false;
			}

			return true;
		} catch (Exception e) {
			return false;
		}
	}

	public int getId() {
		return this.id;
	}
	
	public void setId(int id) {
		this.id = id;
	}
	
	public int getParentId() {
		return this.parent.getId();
	}
	
	public void setParentId(int parentId) {
		this.parent.setId(parentId);
	}
	
	public Category getParent() {
		return this.parent.getObject();
	}
	
	public void setParent(Category parent) {
		this.parent.setObject(parent);
	}
	
	public String getName() {
		return this.name;
	}
	
	public void setName(String name) {
		this.name = name;
	}
	
	public String getCaption() {
		return this.caption;
	}
	
	public void setCaption(String caption) {
		this.caption = caption;
	}
	
	public Set<Category> getChildren() {
		return this.children;
	}
	
	public void setChildren(Set<Category> children) {
		this.children = children;
	}
	
	public CategoryObject getCategoryObject(CategoryObject parent) {
		CategoryObject object = new CategoryObject(this.id, parent, this.name, this.caption);
		return object;
	}

	public CategoryObject getCategoryObjectTree(CategoryObject parent) {
		CategoryObject object = this.getCategoryObject(parent);

		Iterator<Category> iterator = this.children.iterator();
		while (iterator.hasNext()) {
			object.getChildren().add(iterator.next().getCategoryObjectTree(object));
		}
		return object;
	}

}
